/**
 * 220106:增加updateForFrame的time和frame参数
 */
import {EventDispatcher} from 'three';
import Props from "./props";

//场景管理
export default class SceneManager{
    constructor(renderer,size,values={}){
        this._renderer=renderer;
        this._size=size;
        this._oriSize={left:size.left,top:size.top,width:size.width,height:size.height};
        this._eventCenter=new EventDispatcher();
        this._values=values;
        this.init();
    }

    //---------------------------------对外属性及方法-----------------------------

    /**
     * 获取渲染器
     */
    get renderer(){
        return this._renderer;
    }

    /**
     * 获取渲染rect
     * @returns {{top: number, left: number, ratioWidth: number, width: number, ratioHeight: number, height: number}|*}
     */
    get size(){
        return this._rSize;
    }

    /**
     * 设置渲染rect
     * @param size
     */
    set size(size){
        if(size) this._size = size;
        this._rSize.left = this._size.left;
        this._rSize.top = this._size.top;
        this._rSize.width = this._size.width;
        this._rSize.height = this._size.height;
        this._rSize.ratioWidth = this._size.width / this._oriSize.width;
        this._rSize.ratioHeight = this._size.height / this._oriSize.height;
        // this._rSize.pixelRatio=this._renderer.getPixelRatio();

        for(let id in this._spaces){
            this._spaces[''+id+''].resize(this._rSize);
        }

        this._renderer.setSize(this._size.width, this._size.height);
        for (let key in this._resizeCall) {
            if (this._resizeCall['' + key + '']) this._resizeCall['' + key + ''](this._rSize);
        }
        this.fireResizeEvent(this._rSize);
    }

    /**
     * 设置窗体尺寸改变时的回调
     * @param key_funcSize
     */
    set resizeCall(key_funcSize){
        this._resizeCall[''+key_funcSize[0]+'']=key_funcSize[1];
    }

    /**
     * 获取所包含的工作空间
     * @returns {{}|*}
     */
    get childSpace(){
        return this._spaces;
    }

    /**
     * 是否开启抗锯齿
     * @param bol
     */
    set antialias(bol){
        this._props.antialias.value=bol;
    }

    /**
     * 是否自动清除渲染
     * @param bol
     */
    set autoClear(bol){
        this._props.autoClear.value=bol;
    }

    /**
     * 背景颜色
     * @param stringColor
     */
    set colorString(stringColor){
        this._props.colorString.value=stringColor;
    }

    /**
     * 背景透明度
     * @param num
     */
    set colorOpacity(num){
        this._props.colorOpacity.value=num;
    }

    /**
     * 获取当前渲染空间ID
     * @returns {*}
     */
    get renderId(){
        return this._props.renderId.value;
    }

    /**
     * 设置渲染空间ID
     * @param id
     */
    set renderId(id){
        this._props.renderId.value=id;
    }

    /**
     * 是否保留渲染缓冲区数据
     * @param bol
     */
    set preserveDrawingBuffer(bol){
        this._props.preserveDrawingBuffer.value=bol;
    }

    /**
     * 更新渲染调用
     */
    updateForFrame(time,frame) {
        if (!this._props.autoClear.value) this._renderer.clear();
        if(this._props.renderId.value)this._spaces[''+this._props.renderId.value+''].render(this._renderer,time,frame);
    }

    /**
     * 添加到空间渲染队列
     * @param space
     */
    addToQueue(space) {
        for(let id in this._spaces){
            if(id===space.uuid){
                console.warn('cannot add a space to render queue for twice!');
                return;
            }
        }
        this._spaces[''+space.uuid+'']=space;
    }

    /**
     * 从空间渲染队列移除
     * @param space
     */
    removeFromQueue(space){
        if(!this._spaces[''+space.uuid+''])return;
        delete this._spaces[''+space.uuid+''];
    }

    //-----------------------------------私有属性及方法---------------------------------

    initEventBind(){
        this.fireResizeEvent=(size)=>{
            this._resizeEvent.paras.value=size;
            this._eventCenter.dispatchEvent(this._resizeEvent);
        };
        this.fireShiftSceneEvent=(nId,oId)=>{
            this._shiftSceneEvent.paras.current=nId;
            this._shiftSceneEvent.paras.previous=oId;
            this._eventCenter.dispatchEvent(this._shiftSceneEvent);
        };
    }
    init(){
        this.initProps();
        this.initVariable();
        this.initComponents();
        this.initPropCallback();
        this.initEventBind();
    }
    initProps(){
        this._props=new Props({
            antialias:this._values.antialias!==undefined?this._values.antialias:false,
            autoClear:this._values.autoClear!==undefined?this._values.autoClear:true,//..................渲染器是否自动清除
            colorString:this._values.colorString||'#668b8b',//...........................................渲染器背景颜色
            colorOpacity:this._values.colorOpacity!==undefined?this._values.colorOpacity:1,//.................................................渲染器背景颜色透明度
            renderId:this._values.renderId!==undefined?this._values.renderId:null,//.....................渲染场景索引
            preserveDrawingBuffer:this._values.preserveDrawingBuffer||false,//...........................是否保留绘制缓存
        });
    }
    initPropCallback(){
        this._props.onKeyChange('antialias',(_new,_old)=>{
            this._renderer.antialias=_new;
        });
        this._props.onKeyChange('preserveDrawingBuffer',(_new,_old)=>{
            this._renderer.preserveDrawingBuffer=_new;
        });
        this._props.onKeyChange('autoClear',(_new,_old)=>{
            this._renderer.autoClear=_new;
        });
        this._props.onKeyChange('colorString',(_new,_old)=>{
            this._renderer.setClearColor(_new,this._props.colorOpacity.value);
        });
        this._props.onKeyChange('colorOpacity',(_new,_old)=>{
            this._renderer.setClearColor(this._props.colorString.value,_new);
        });
        this._props.onKeyChange('renderId',(_new,_old)=>{
            this.fireShiftSceneEvent(_new,_old);
        });
    }
    initVariable(){
        this._spaces={};
        this._resizeCall={};
        this._rSize = {left:0, top:0, width:0, height:0, ratioWidth:1, ratioHeight:1,pixelRatio:1};
        this._resizeEvent={type:'onResize',paras:{value:null}};
        this._shiftSceneEvent={type:'onShiftScene',paras:{current:null,previous:null}};
    }
    initComponents(){
        this.initRenderer();
        this.initSize();
    }
    initRenderer(){
        this._renderer.setClearColor(this._props.colorString.value,this._props.colorOpacity.value);
        this._renderer.autoClear=this._props.autoClear.value;
        this._renderer.antialias=this._props.antialias.value;
        this._renderer.preserveDrawingBuffer=this._props.preserveDrawingBuffer.value;
    }
    initSize(){
        this._rSize.left = this._size.left;
        this._rSize.top = this._size.top;
        this._rSize.width = this._size.width;
        this._rSize.height = this._size.height;
        this._rSize.ratioWidth = this._size.width / this._oriSize.width;
        this._rSize.ratioHeight = this._size.height / this._oriSize.height;
        this._rSize.pixelRatio = this._renderer.getPixelRatio();
    }
}